package com.fbb.codegen.main;

import java.sql.Connection;
import java.util.Properties;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.fbb.codegen.core.CodeGen;
import com.fbb.codegen.mysql.MysqlCodeGen;

public class GenCodeStart {

	public static Connection getConnections(String driver, String url, String user, String pwd) {
		try {
//			Class.forName(driver);
//			Connection connection = DriverManager.getConnection(url, user, pwd);
//			return connection;
			
			
			Properties prop0 =new Properties();
			prop0.setProperty("url",url);
			prop0.setProperty("username",user);
			prop0.setProperty("password",pwd);
			prop0.setProperty("driverClassName",driver);
			prop0.setProperty("type","com.alibaba.druid.pool.DruidDataSource");
			DruidDataSource ds = (DruidDataSource) DruidDataSourceFactory.createDataSource(prop0);
			
			Properties prop1 = new Properties();
			prop1.setProperty("remarks", "true");
			prop1.setProperty("useInformationSchema", "true");
			ds.setConnectProperties(prop1);
			
			return ds.getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
//		final String url = "jdbc:mysql://172.16.101.130:3306/ehr?characterEncoding=utf-8&useUnicode=true&zeroDateTimeBehavior=convertToNull";
//		final String driver = "com.mysql.jdbc.Driver";
//		final String user = "eHRAdmin";
//		final String pwd = "shangde_wings";
		final String url = "jdbc:mysql://127.0.0.1:3306/ehr_message?characterEncoding=utf-8&useUnicode=true&zeroDateTimeBehavior=convertToNull";
		final String driver = "com.mysql.jdbc.Driver";
		final String user = "root";
		final String pwd = "fan1988";

		final String fileOutPath = "D:/tmp";
		final String packageName = "com.ybwh.springboot1.model.po";
		final String author = "Fan Beibei";

		try {
			System.out.println("start gen code .....");
			Connection conn = getConnections(driver, url, user, pwd);
			CodeGen codeGen = new MysqlCodeGen(conn);
			codeGen.createAllTableEntityFile(fileOutPath, packageName, author,"t_");
			System.out.println("gen code  success!!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
